package com.mingchuang.administrator.mingchuang.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mingchuang.administrator.mingchuang.AppApplication;
import com.mingchuang.administrator.mingchuang.zip.ZipUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by Administrator on 2016/1/11.
 */
public class Constant {
    // 数据库名称
    public static final String DB_NAME = "MARACANA_DB.db";


    public static final String SPNAME = "AppConfig";

    static{
        File file = new File(getPath());
        if(!file.exists()){
            file.mkdir();
        }
    }


    public static String getPath(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/MC";
        Log.d("kipeng","path:"+path);
        return path;
    }
    public static String getPhotoPath(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/MC/photo";
        return path;
    }

    public static String getReceivedPath(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/MC/received";
        return path;
    }

    public static String getreUnzipPath(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/MC/unzip";
        return path;
    }



    public  static void  upload(final String path , final Context context ,final int id){

        RequestParams params = new RequestParams();
        //params.addHeader("name", "value");
        //params.addQueryStringParameter("name", "value");

        // 只包含字符串参数时默认使用BodyParamsEntity，
        // 类似于UrlEncodedFormEntity（"application/x-www-form-urlencoded"）。
        //params.addBodyParameter("name", "value");

        // 加入文件参数后默认使用MultipartEntity（"multipart/form-data"），
        // 如需"multipart/related"，xUtils中提供的MultipartEntity支持设置subType为"related"。
        // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity（如：
        // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity）。
        // 例如发送json参数：params.setBodyEntity(new StringEntity(jsonStr,charset));

        //String path = Constant.getPath()+"/received/Y12_4.7z";
        params.addBodyParameter("file", new File(path+".zip"));
        HttpUtils http =  new HttpUtils();

        final ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle("正在上传");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setCancelable(false);
        dialog.setMax((int) new File(path).length());
        dialog.setCanceledOnTouchOutside(false);
        http.send(HttpRequest.HttpMethod.POST, "http://192.168.1.101/mc/upload/upload", params, new RequestCallBack<String>() {


            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("kipeng","onSuccess"+responseInfo.toString());
                //responseInfo.result;
               // responseInfo.statusCode;
                dialog.dismiss();
                DbUtils db = AppApplication.getDb();
                try {
                    //db.save(pileInfo);
                    String sql = "update PileInfoBean set isUpload = '1'where id= "+id;
                    //UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'

                    db.execNonQuery(sql);
                } catch (DbException e) {
                    e.printStackTrace();
                }

                Toast.makeText(context ,"上传成功",Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                dialog.setProgress((int) current);

                Log.d("kipeng","onLoading:"+total+"/"+current+":"+isUploading);
            }

            @Override
            public void onStart() {
                super.onStart();
                Log.d("kipeng","onStart");
                dialog.show();
            }



            @Override
            public void onFailure(HttpException e, String s) {
                Log.d("kipeng","onFailure:"+s+"/"+e.getMessage());
                dialog.dismiss();
                Toast.makeText(context ,"上传失败，请稍后再试",Toast.LENGTH_SHORT).show();

            }
        });





    }


}
